


/*

* This file uses DHS data. It uses:

* - standard DHS household data from IPUMS on subsaharand Africa from 2003 to 2016
* - a file that matches DHS coordinates to countries

REMARK: as before, to make clear how many observations are used for the border fixed effects estimations, we remove all observations for which we do not observe an other side of a border 

*/



local nametable = "tables/rev_dhs.tex"



* This file matches point_x point_y coordinates to DHS observations
insheet using "private_data/dhs_merge.txt", clear
keep point_x point_y dhs*
destring point_x, replace
destring point_y, replace
sort dhsid
qui save "private_data/g3.dta", replace



use "private_data/idhs_00035.dta", clear

gen 	ncountry 	 = ""
replace ncountry = "angola" if country==24
replace ncountry = "burundi" if country==108
replace ncountry = "cameroon" if country==120
replace ncountry = "rdc" if country==180
replace ncountry = "benin" if country==204
replace ncountry = "ethiopia" if country==231
replace ncountry = "ghana" if country==288
replace ncountry = "guinea" if country==324
replace ncountry = "cotedivoire" if country==384
replace ncountry = "kenya" if country==404
replace ncountry = "lesotho" if country==426
replace ncountry = "malawi" if country==454
replace ncountry = "mali" if country==466
replace ncountry = "morocco" if country==504
replace ncountry = "mozambique" if country==508
replace ncountry = "namibia" if country==516
replace ncountry = "niger" if country==562
replace ncountry = "nigeria" if country==566
replace ncountry = "rwanda" if country==646
replace ncountry = "senegal" if country==686
replace ncountry = "southafrica" if country==710
replace ncountry = "zimbabwe" if country==716
replace ncountry = "uganda" if country==800
replace ncountry = "egypt" if country==818
replace ncountry = "tanzania" if country==834
replace ncountry = "burkinafaso" if country==854
replace ncountry = "zambia" if country==894


drop country
rename ncountry country
sort dhsid
merge m:1 dhsid using "private_data/g3.dta"

keep if _merge==3
drop _merge

sort point_x point_y
save "private_data/g3.dta", replace
	

	
use "public_data/g30.dta", clear

keep point_x point_y ethnic_name varofi border_id adist rdist1in rdist1out
sort point_x point_y
merge 1:m point_x point_y using "private_data/g3.dta"
keep if _merge==3
drop _merge

*keeps only observations on both sides of a border
bys border_id: egen intermin = min(varofi)
bys border_id: egen intermax = max(varofi)
drop if intermin==intermax
drop inter*

bys sample hhid: egen hsize = sum(1)
gen lhsize = ln(hsize)


replace bankacc   = . if bankacc>1
replace electrchh = . if electrchh>1
replace fridge = . if fridge>1
replace mobphone = . if mobphone>1


egen groupfe 	= group(border_id ethnic_name)
save "private_data/g3.dta", replace



use "private_data/g3.dta", clear



forvalues xd=1(1)1{
	
	if `xd'==1{
		local xdist = 50
	}

	
	*----
	eststo clear
	local column_number = 1
	local column_name = "column" + "`column_number'"
	*---



	
	foreach varl of varlist livestockyn fridgehh mobphone electrchh bankacc  {

	use "private_data/g3.dta", clear

	drop if adist>`xdist'

	gen weight = 1
	keep if hhrelate==1
	drop if `varl'>1
	
	
	*keeps year with largest sample of observations for country
	capture drop inter*
	bys sample: egen inters = sum(1)
	bys country: egen intermax = max(inters)
	keep if intermax==inters
	capture drop inter*
	

	gen interdep = `varl'>0
		
		*computes mean of dependent var
		egen interm = mean(interdep)
		local vmeandep = interm[1]
		local meandep = string(`vmeandep')
		if index("`meandep'",".")==1{
			local meandep = "0"+substr("`meandep'",1,3)
		}
		if index("`meandep'",".")<5&index("`meandep'",".")>1{
			local meandep = substr("`meandep'",1,4)
		}
		di "`meandep'"

		*computes the number of country pairs
		egen intercp = group(border)
		egen intercpmax = max(intercp)
		local countrypair = intercpmax[1]
		drop intercp*
	
	
	local sureglist = "`varl'" + subinstr(" livestockyn fridgehh mobphone electrchh bankacc","`varl'","",.)
	di "`sureglist'"
	qui xi: sureg (`sureglist' =  varofi  rdist1* i.groupfe ) [aw=weight], small dfk
	local suregse = e(V)[1,1]^.5
	local suregsewb = "[0" + substr("`suregse'",1,4) + "]"
	
*	xi: areg interdep varofi rdist1* [aw=weight]	,a(groupfe) cluster(border)
	reghdfe interdep rdist1*   varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local space  					" "
	estadd local subsample				"All"
	estadd local maxdist 					"`xdist'"
	estadd local polynomial				"Linear"
	estadd local countrypair 					"`countrypair'"
	estadd local nmeandep			"`meandep'"
	estadd local groupfe				"Yes"
	estadd local othercov				"Yes"
	estadd local nsuregse				"`suregsewb'"
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"
	
	
	drop inter*
	keep if aglandyn==1
	
	gen interdep = `varl'>0
	egen interm = mean(interdep)
	local vmeandep = interm[1]
	local meandep = string(`vmeandep')
	if index("`meandep'",".")==1{
		local meandep = "0"+substr("`meandep'",1,3)
	}
		
	local sureglist = "`varl'" + subinstr(" livestockyn fridgehh mobphone electrchh bankacc","`varl'","",.)
	di "`sureglist'"
	qui xi: sureg (`sureglist' =  varofi  rdist1* i.groupfe ) [aw=weight], small dfk
	local suregse = e(V)[1,1]^.5
	local suregsewb = "[0" + substr("`suregse'",1,4) + "]"
	
	

*	xi: areg interdep rdist1*  varofi  [aw=weight]	,a(groupfe) cluster(border)
	reghdfe interdep rdist1*   varofi [aw=weight], absorb(groupfe) vce(cluster groupfe country)
	eststo `column_name'
	estadd local space  					" "
	estadd local subsample				"Own land"
	estadd local maxdist 					"`xdist'"
	estadd local polynomial				"Linear"
	estadd local countrypair 					"`countrypair'"
	estadd local nmeandep			"`meandep'"
	estadd local groupfe				"Yes"
	estadd local othercov				"Yes"
	estadd local nsuregse				"`suregsewb'"
	local column_number = `column_number' + 1
	local column_name = "column" + "`column_number'"


	
	
	}

*	
label var interdep  " "		


#delimit ;
esttab column1 column2 column3 column4 column5 column6 column7 column8 column9 column10 
using "`nametable'", replace
label
b(3)
se(3)
star(* 0.10 ** 0.05 *** 0.01)
width(1.0\hsize)
nonotes
nolines
noconstant
mgroups(   " \textit{owns livestock}" " \textit{owns fridge}" " \textit{owns cell phone} "  " \textit{has electricity}" " \textit{has bank account}" , pattern(1 0 1 0 1 0 1 0 1 0)
prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) 
 keep(varofi)
order(varofi)
  stats(nsuregse			groupfe				space	polynomial		space	r2		N					countrypair	 		space	maxdist							space	subsample				space	nmeandep,
    fmt(0				0					0		0				0		3		0					0			 		0		0								0		0						0		0)
 layout(@				@					@		@				@		@		@					@			 		@		@								@		@						@		@)	 	
labels("SUR s.e."		"Border x Ethnic FE"	" "		"RD Polynomial"	" "		"R2"	"\# Observations"	"\# Country pairs"	" "		"Max distance to border (km)"	" " 	"Sample of households" " "		"Mean Dep. Var.")
 )	
noobs
booktabs
;
#delimit cr


}
